﻿(function ($, undefined) {

    // define the name of the UI component
    var ui_name = "ezTips"

    // initialize object
    var eztips = $.eztips = {

        // default values
        defaults: {
    },

    setDefaults: function (defaultOptions) {
        this.defaults = $.extend({}, this.defaults, defaultOptions);
    }
};

$.fn.eztips = function (options) {

    // method calling
    if (typeof options == 'string') {
        var args = Array.prototype.slice.call(arguments, 1),
			res;
        this.each(function () {
            var data = $.data(this, ui_name);
            if (data) {
                var meth = data[options];
                if (meth) {
                    var r = meth.apply(this, args);
                    if (res === undefined) {
                        res = r;
                    }
                }
            }
        });
        if (res !== undefined) {
            return res;
        }
        return this;
    }

    // initialize options
    options = $.extend({}, eztips.defaults, options);

    /* globle vars 
    -----------------------------------------------------------------------------*/
    var globle = {

};

this.each(function () {
    var _self = $(this);

    showTip(_self);

    // initialize callback
    for (var key in options) {
        var value = options[key];
        if (typeof value == 'function') {
            // bind callbacks
            $(_self).bind(key, value);
        }
    }


    /* Private Methods
    -----------------------------------------------------------------------------*/
    function showTip () {
        if (!_self.data("isVisiable")) {
            var _tipsBox = $('#tips');
            var _tipsw = $('#tipsw');
            var _contentw = $('#contentw');

            _tipsw.fadeIn();
            
            var tipCount = $(document).data('tipsCount');
            $(document).data('tipsCount', ++ tipCount);
        
            EzDesk.DesktopWeb.RefreshPageStyle(tipCount);

            $('<div class="tip"/>')
                    .append(_self.show())
                    .appendTo(_tipsBox);

            _self.data("isVisiable", true);
        
        }
    }


    function hideTip() {
         if (_self.data('isVisiable')) {
            var $parent = $(this).parent()
            _self.appendTo('#root').hide();
            $parent.remove();

            var tipCount = $(document).data('tipsCount');
            $(document).data('tipsCount', --tipCount);
            EzDesk.DesktopWeb.RefreshPageStyle(tipCount);

            _self.data('isVisiable', false);
        }

    }


    
    /* Callback Triggers
    -----------------------------------------------------------------------------*/



    /* Public Methods
    -----------------------------------------------------------------------------*/

    var publicMethods = {
        hide: function () {
            hideTip();   
        },


    };

    $.data(this, ui_name, publicMethods);
});

return this;
};



})(jQuery);
$(function () {
    $(document).data('tipsCount', 0);
}) 

$(function(){
    EzDesk.DesktopWeb.RefreshPageStyle();
})